package com.secsys.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.secsys.dto.SearchDTO;
import com.secsys.model.TbSysUser;
import com.secsys.service.SearchService;
import com.secsys.service.UserService;
import com.secsys.util.Constant;
import com.secsys.util.MySqlUtil;

public class SearchAction extends BaseAction<SearchDTO> {
	private SearchDTO searchDTO;
	private SearchService searchService;
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	private static final Logger logger = Logger.getLogger(SearchAction.class);

	/**
	 * @Description 基本搜索
	 * @return
	 * @throws Exception
	 */
	public String base() throws Exception {

		String hql = MySqlUtil.basicMatchHQL(searchDTO.getSearchType(),
				Constant.TbBbsPost);
		List searchList=null;
         if(searchDTO.getKeyWord()!=null&&searchDTO.getSearchType().equals("author")){
        	    TbSysUser user = userService.queryUserByName(searchDTO.getKeyWord());
        	    searchList = searchService.queryPostByParam(hql, user);
         }else{
        	 searchList = searchService.queryPostByParam(hql, MySqlUtil
     				.likeMatchParaten(searchDTO.getKeyWord()));
         }
		session.put("searchList", searchList);
		return "searchQuery";
	}
    
	public String advancedSearch()throws Exception{
		System.out.println("***********"+searchDTO.getKeyWord()+"|"+searchDTO.getModuleScope()+"|"+searchDTO.getSearchTime1()+"|"+searchDTO.getSearchTime2()+"|"+searchDTO.getSearchType()+"|"+searchDTO.getSort()+"******");
		  MySqlUtil.advMatchHQL(searchDTO.getSearchType(), Constant.TbBbsPost, searchDTO.getSearchTime1(), searchDTO.getSearchTime2(), searchDTO.getSort(), null);
		return "searchQuery";
	}
	
	
	public SearchDTO getSearchDTO() {
		return searchDTO;
	}

	public void setSearchDTO(SearchDTO searchDTO) {
		this.searchDTO = searchDTO;
	}

	@Override
	public SearchDTO getModel() {
		// TODO Auto-generated method stub
		if (searchDTO == null) {
			searchDTO = new SearchDTO();
		}

		return searchDTO;
	}

}
